package so.shanku.essential.utils;

import android.os.Environment;
import so.shanku.essential.BuildConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LogUtil {
	public static final boolean isDebug = true;

	public static void e(String info) {
		android.util.Log.e("TnAcr", info);
		if (isDebug) {
			write("TnAcr ", info);
		}
	}

	private static void write(String tag, String info) {
		try {
			// 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
			if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				return;
			}
			String path = Confing.productPath + "log.log";
			File file = new File(path);
			if (!file.exists() || file.isDirectory()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			FileWriter writer = new FileWriter(path, true);
			writer.write(System.currentTimeMillis() + "  " + tag + "  " + info + "\n");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void i(String tag, String info) {

		android.util.Log.i(tag, info);
		if (isDebug) {
			write(tag, info);
		}
	}

	public static void i(String tag, Object obj) {

		android.util.Log.i(tag, obj.toString());
		if (isDebug) {
			write(tag, obj.toString());
		}
	}

	public static void e(String tag, String info) {

		android.util.Log.e(tag, info);
		if (isDebug) {
			write(tag, info);
		}
	}

	public static void e(String tag, Object obj) {

		android.util.Log.e(tag, obj.toString());
		if (isDebug) {
			write(tag, obj.toString());
		}
	}

	public static void d(String tag, String info) {

		android.util.Log.d(tag, info);
		if (isDebug) {
			write(tag, info);
		}
	}

	public static void d(String tag, Object obj) {

		android.util.Log.d(tag, obj.toString());
		if (isDebug) {
			write(tag, obj.toString());
		}
	}

	public static void w(String tag, String info) {

		android.util.Log.w(tag, info);
		if (isDebug) {
			write(tag, info);
		}
	}

	public static void w(String tag, Object obj) {

		android.util.Log.w(tag, obj.toString());
		if (isDebug) {
			write(tag, obj.toString());
		}
	}

	public static void v(String tag, String info) {

		android.util.Log.v(tag, info);
		if (isDebug) {
			write(tag, info);
		}
	}

	public static void v(String tag, Object obj) {

		android.util.Log.v(tag, obj.toString());
		if (isDebug) {
			write(tag, obj.toString());
		}
	}

}
